package com.iamk.feature;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;

import com.iamk.util.LoadGMM;
import com.jMEF.gmm.MixtureModel;
import com.jMEF.gmm.PVector;

// Input list vetors features output r - label each images;
public class ImagesRelevance {
	ArrayList<float[]> featuresRegion;
	double[][] density;
	float[][] PNi_Wk;
	double[][] Pwm_wk;
	double[][] Pwk_BiNi;
	ArrayList<MixtureModel> mixArrayList;
	ArrayList<String> label;

	public ImagesRelevance(ArrayList features) {
		// TODO Auto-generated constructor stub
		this.featuresRegion = features;
		label=new ArrayList<String>();
		init();
		PNi_Wk = new float[featuresRegion.size()][mixArrayList.size()];
		Pwm_wk = new double[mixArrayList.size()][mixArrayList.size()];
		for (int i = 0; i < mixArrayList.size(); i++) {
			for (int j = 0; j < mixArrayList.size(); j++) {
				Pwm_wk[i][j] = Math.random();
			}
		}
	}

	// Computer desity p(xij|wm)
	private void init() {
		mixArrayList = LoadGMM.getMixtureModels();
		density = new double[featuresRegion.size()][mixArrayList.size()];
		for (int i = 0; i < featuresRegion.size(); i++) {
			for (int j = 0; j < mixArrayList.size(); j++) {
				density[i][j] = mixArrayList.get(j).density(subArr(featuresRegion.get(i)));
				if(density[i][j]<Math.pow(10, 6)){
					density[i][j]=0;
				}
			}
		}
	}

	// p(Ni|wk)=jmp(xij |wm)p(wm|wk)
	private void computerPNi_Wk() {
		for (int i = 0; i < featuresRegion.size(); i++) {
			for (int j = 0; j < mixArrayList.size(); j++) {
				PNi_Wk[i][j] = multiplica(i, j);
			}
		}
	}

	// k region
	private float multiplica(int k, int j) {
		float result = 1;
		for (int i = 0; i < featuresRegion.size(); i++) {
			if (k != i) {
				if(sum(i,j)>0){
				result *= sum(i, j);}
			}
		}
		return result;
	}
	
	private double sum(int i, int k) {
		double result = 0d;
		for (int j = 0; j < mixArrayList.size(); j++) {
			result += density[i][j] * Pwm_wk[j][k];
		}
		return result;
	}


	public float[][] getPNi_Wk() {
		computerPNi_Wk();
		return PNi_Wk;
	}

	private PVector subArr(float[] arr) {
		PVector pv = new PVector(arr.length - 1);
		double[] result = new double[arr.length - 1];
		for (int i = 0; i < arr.length - 1; i++) {
			result[i] = arr[i];
		}
		pv.array = result;
		return pv;
	}
	// Computer p(wk|Bi, Ni) ~ p(x|wk)p(Ni|wk)p(wk);
	private void densityLabel(){
		getPNi_Wk();
		for (int i = 0; i < featuresRegion.size(); i++) {
			double valueMax=0,temp=0;
			String strlabel=null;
			for (int j = 0; j < mixArrayList.size(); j++) {
//				density[i][j] = mixArrayList.get(j).density(subArr(featuresRegion.get(i)));
				temp=density[i][j]*PNi_Wk[i][j]*mixArrayList.get(j).weighLabel;
				if(temp>valueMax){
					valueMax=temp;
					strlabel=" "+(j+1);
				}
			}
			System.out.println(valueMax + " " + strlabel);
			// Check them label o day nua la ok
			label.add(strlabel);
		}
	}
	public ArrayList<String> getLabel() {
		densityLabel();
		return label;
	}
	// Label Images
	public static void main(String[] args) {
		BufferedImage img;
		try {
			img = ImageIO.read(new File("C:\\Users\\sev_user\\Desktop\\datatest\\3_24_s.bmp"));
//			FColorMSCR fColorMSCR = new FColorMSCR(img,"C:\\Users\\sev_user\\Desktop\\datatest\\3_24_s.txt",0);
			FColorMSCR fColorMSCR = new FColorMSCR(img,"test.txt","");
			ImagesRelevance images=new ImagesRelevance(fColorMSCR.extract());
			
//			System.out.println(images.getPNi_Wk().length);
//			for(int i=0;i<images.getPNi_Wk().length;i++){
//				for(int j=0;j<images.getPNi_Wk()[0].length;j++)
//				System.out.print(images.getPNi_Wk()[i][j]+" ");
//				System.out.println();
//			}
			images.getLabel();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
